package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// UpgradeSchedule 升级计划
type UpgradeSchedule struct {
	ID             string `json:"id" gorm:"column:id;primaryKey;type:bigint;size:64;comment:主键"`
	Name           string `json:"name" gorm:"column:name;type:string;size:30;comment:升级计划名称"`
	TerminalType   string `json:"terminal_type" gorm:"column:terminal_type;type:string;size:100;comment:终端类型"`
	UpgradeVersion string `json:"upgrade_version" gorm:"column:upgrade_version;type:string;size:40;comment:升级的版本"`
	ForceUpgrade   int64  `json:"force_upgrade" gorm:"column:force_upgrade;type:int;size:8;comment:强制升级"`
	AutoUpgrade    int64  `json:"auto_upgrade" gorm:"column:auto_upgrade;type:int;size:8;default:1;commnet:自动安装"`
	StartTime      int64  `json:"start_time" gorm:"column:start_time;type:int;size:64;comment:开始时间"`
	EndTime        int64  `json:"end_time" gorm:"column:end_time;type:int;size:64;comment:结束时间"`
	Mode           int64  `json:"mode" gorm:"column:mode;type:int;size:8;default:1;comment:计划模式，1-正常模式，2-压测模式"`
	CreatedAt      int64  `json:"created_at" gorm:"autoCreateTime;column:created_at;type:int;size:64;comment:创建时间"`
	UpdatedAt      int64  `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:true;type:int;size:64;comment:更新时间"`
}

// TableName 表名
func (UpgradeSchedule) TableName() string {
	return "upgrade_schedule"
}

// BeforeCreate 自增id
func (u *UpgradeSchedule) BeforeCreate(tx *gorm.DB) (err error) {
	if u.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	u.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &UpgradeSchedule{})
}
